<?php
class JddjCronModel
{
    public $chnId;
    public $reqSyncProductLimit; //同步商品频率限制值

    public function __construct(){
        $this->chnId = 2;

        $this->reqSyncProductLimit = 200;
    }

    /*
        同步商品信息
        params
            store_id 店铺id
            other_category_id 第三方分类id
            product_id 快方商品id
    */
    public function syncProduct($store_id = 0, $other_category_id = '', $product_id = 0)
    {
        $productInfo = getChannelProductInfo($this->chnId, $store_id, $product_id);

        if(empty($productInfo) == false){
            return false;
        }
        $JddjApiModel = new JddjApiModel();
        $addProduct = $JddjApiModel->addSku($store_id, $product_id, $other_category_id);
        
        $this->incrSyncProductLimit("JINGDONGDJ", $store_id);

        return true;
    }

    /*
        同步商品库存
        params
            sotck_info[] 同步商品库存数组对象
            [
                [
                    "store_id" => "3",
                    "other_product_id" => "1507712346147601",
                    "product_stock" => "8",
                ]
            ]
    */
    public function syncStock($sotck_info = array())
    {
        if(empty($sotck_info)){
            return false;
        }
        $JddjApiModel = new JddjApiModel();

        $syncStock = $JddjApiModel->syncStock($sotck_info);
        $this->incrSyncProductLimit("JINGDONGDJ", "STOCK");
    }

    /*
        增加提交限制次数
        params
            chn_code 渠道代码
            store_id 店铺id
    */
    public function incrSyncProductLimit($chn_code = '', $store_id = '')
    {
        $time = date("YmdHi"); //限制维度为分钟
        $key = sprintf("KCS_QUEUE_%s_SYNC_PRODUCT_LIMIT_%s_%s", $chn_code, $store_id, $time);

        $MyRedis = new MyRedis();

        $ttl = $MyRedis->TTL($key);

        if($ttl < 0){
            $MyRedis->SET($key, 0);
            $MyRedis->EXPIRE($key, 60); //以防万一 设置为时间多一点点
        }

        $MyRedis->INCR($key);
    }

    /*
        获取提交次数限制
        params
            chn_code 渠道代码
            store_id 店铺id
    */
    public function getSyncProductLimit($chn_code = '', $store_id = '')
    {
        $time = date("YmdHi"); //限制维度为分钟
        $key = sprintf("KCS_QUEUE_%s_SYNC_PRODUCT_LIMIT_%s_%s", $chn_code, $store_id, $time);

        $MyRedis = new MyRedis();

        $reqCnt = $MyRedis->get($key);

        return $reqCnt < $this->reqSyncProductLimit ? true : false;
    }
    
}
?>